#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/1/15 18:00
# @Author : 闫峰
# @File    : view.py
import datetime
import json
from urllib import parse
import random
import re
import time
import uuid
from flask import request, Response, jsonify, Request

from service.charge.v1_apis import ChargeApis
from utils.error_eume import ArchiveError
from utils.functions_control import get_uuid
from utils.handler_request_log import handler_request_log
from utils.mongodb_utils import MongoDBLink
from utils.redis_operations import RedisOperator


user = parse.quote_plus("viviu")
passwd = parse.quote_plus("viu@1234")
sit_charge_mock_data_modb = MongoDBLink(
    uri=f'mongodb://{user}:{passwd}@10.86.3.113:27017/?authSource=admin',
    dbname='sit_charge_mock_data', coll_name='sit_charge_mock_data')

def _ArchiveService_GetGameArchiveConfig0_HTTP_Handler():
    is_forward = True

    path = "/v1/archive/" + request.path.split("/")[-1]
    # 使用示例
    redis_op = RedisOperator()
    value = redis_op.get_key(path)
    if value:
        is_forward = False
    sit_base_url = "http://10.141.194.31:32124" + path
    request_method = request.method
    if request_method in ["DELETE","GET"]:
        request_json = None
    else:
        request_json = request.json
    resp_json = None
    response = None
    if is_forward:
        status_code,resp_json = ChargeApis.charge_common_handler(request_method, sit_base_url, request_json)
        response = Response(json.dumps(resp_json), status=200, mimetype='application/json')
    else:
        # resp_json = {
        #     "ret": {
        #         "code": 0,
        #         "reason": "",
        #         "msg": ""
        #     },
        #     "body": {
        #         "id": "27569",
        #         "idc": "202",
        #         "status": 14,
        #         "version": "40",
        #         "isValid": True
        #     }
        # }
        if value == ArchiveError.SERVICE_ERROR.value:
            status_code = random.choice([500])

            match status_code:
                case 500:
                    resp_json = {
                        "ret": {
                            "code": status_code,
                            "reason": f"{path}调用失败",
                            "msg": f"{path}调用失败",
                            "request_id": str(get_uuid()),
                        }
                    }
                    resp_str = json.dumps(resp_json)
                    response = Response(resp_str, status=status_code, mimetype='application/json')
        else:
            status_code, resp_json = ChargeApis.charge_common_handler(request_method, sit_base_url, request_json)
            resp_dict = resp_json
            resp_dict["body"]["isValid"] = False
            resp_str = json.dumps(resp_json)
            response = Response(resp_str, status=status_code, mimetype='application/json')
    handler_request_log(request,status_code,resp_json,sit_charge_mock_data_modb,True)
    return response
